package com.gpframework.module.controller.ctn;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.utils.poi.ExcelUtil;
import com.gpframework.module.ctn.domain.GpTelexRelease;
import com.gpframework.module.ctn.domain.req.GpTelexReleaseAdd;
import com.gpframework.module.ctn.domain.req.GpTelexReleaseReq;
import com.gpframework.module.ctn.domain.req.GpTelexReleaseUpdate;
import com.gpframework.module.ctn.domain.vo.GpTelexReleaseExcel;
import com.gpframework.module.ctn.domain.vo.GpTelexReleaseVo;
import com.gpframework.module.ctn.service.IGpNigeriaMarketCheckService;
import com.gpframework.module.ctn.service.IGpTelexReleaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
/**
 * @ClassName GpTelexReleaseController
 * @Author gpframework
 * @Date 2024-01-08
 * @Version 1.0.0
 * @Description 装柜电放填写Controller
 */
@Validated
@Api(tags = "装柜电放填写")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/ctn/telexrelease" )
public class GpTelexReleaseController extends BaseController {

    private final IGpTelexReleaseService iGpTelexReleaseService;
    private final IGpNigeriaMarketCheckService iGpNigeriaMarketCheckService;


 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2024-01-08
  * @Param gpTelexRelease
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('ctn:telexrelease:list')")
    @GetMapping("/list")
    public TableDataInfo<GpTelexReleaseVo> list(GpTelexReleaseReq gpTelexReleaseReq)
    {
        startPage();
        QueryWrapper<GpTelexRelease> lqw = new QueryWrapper<GpTelexRelease>();
        gpTelexReleaseReq.generatorQuery(lqw,true);
        List<GpTelexReleaseVo> list = iGpTelexReleaseService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpTelexRelease
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('ctn:telexrelease:add')" )
    @Log(title = "添加装柜电放填写" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpTelexReleaseAdd gpTelexReleaseAdd) {
        GpTelexRelease gpTelexRelease = Convert.convert(new TypeReference<GpTelexRelease>() {}, gpTelexReleaseAdd);
        if (gpTelexReleaseAdd.getTelexReleaseId()==null){
            return toAjax(iGpTelexReleaseService.save(gpTelexRelease) ? 1 : 0);
        }
        gpTelexRelease.setId(gpTelexReleaseAdd.getTelexReleaseId());
        return toAjax(iGpTelexReleaseService.updateById(gpTelexRelease) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpTelexRelease
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('ctn:telexrelease:edit')" )
    @Log(title = "修改装柜电放填写" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GpTelexReleaseUpdate gpTelexReleaseUpdate) {
        GpTelexRelease gpTelexRelease = Convert.convert(new TypeReference<GpTelexRelease>() {}, gpTelexReleaseUpdate);
        return toAjax(iGpTelexReleaseService.updateById(gpTelexRelease) ? 1 : 0);
    }

    /**
     * @Description 修改装柜货柜管理
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpTelexRelease
     * @Return TableDataInfo
     */
    @ApiOperation("修改装柜货柜管理")
    @PreAuthorize("@ss.hasPermi('ctn:telexrelease:edit')" )
    @Log(title = "修改装柜货柜管理" , businessType = BusinessType.UPDATE)
    @PutMapping("/editCtnManagement")
    public AjaxResult editCtnManagement(@Valid @RequestBody GpTelexReleaseUpdate gpTelexReleaseUpdate) {
        return toAjax(iGpTelexReleaseService.editCtnManagement(gpTelexReleaseUpdate) ? 1 : 0);
    }


    /**
     * @Description 导出装柜电放填写模板
     * @Author gpframework
     * @Date 2023-05-24
     * @Param gpCustomer
     * @Return TableDataInfo
     */
    @ApiOperation("导出装柜电放填写模板")
    @Log(title = "导出装柜电放填写模板" , businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('ctn:telexrelease:importTemplate')" )
    @GetMapping("/importTemplate" )
    public AjaxResult exportTemplate() {
        ExcelUtil<GpTelexReleaseExcel> util = new ExcelUtil<>(GpTelexReleaseExcel.class);
        return util.exportExcel(GpTelexReleaseExcel.list(), "模板");
    }

    /**
     * @Description 导入装柜电放填写
     * @Author gpframework
     * @Date 2023-10-27
     * @Param (GpMember
     * @Return TableDataInfo
     */
    @Log(title = "导入装柜电放填写" , businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('ctn:telexrelease:import')" )
    @PostMapping("/import" )
    public AjaxResult entityImport(MultipartFile file, Long brandId, Long storeId) throws Exception {
        ExcelUtil<GpTelexReleaseExcel> util = new ExcelUtil<GpTelexReleaseExcel>(GpTelexReleaseExcel. class);
        List<GpTelexReleaseExcel> excelList = util.importExcel(file.getInputStream());

        return AjaxResult.builder(() -> {
            return this.iGpTelexReleaseService.gpTelexReleaseImport(excelList, brandId, storeId);
        });
    }

    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpTelexRelease
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('ctn:telexrelease:export')" )
    @Log(title = "导出装柜电放填写" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(GpTelexReleaseReq gpTelexReleaseReq) {
        QueryWrapper<GpTelexRelease> lqw = new QueryWrapper<GpTelexRelease>();
        gpTelexReleaseReq.generatorQuery(lqw,true);
        List<GpTelexReleaseVo> list = iGpTelexReleaseService.listVo(lqw);
        ExcelUtil<GpTelexReleaseVo> util = new ExcelUtil<GpTelexReleaseVo>(GpTelexReleaseVo. class);
        return util.exportExcel(list, "telexrelease" );
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpTelexRelease
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('ctn:telexrelease:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GpTelexReleaseVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGpTelexReleaseService.getVoById(id));
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpTelexRelease
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('ctn:telexrelease:remove')" )
    @Log(title = "删除装柜电放填写" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpTelexReleaseService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }


    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpTelexRelease
     * @Return TableDataInfo
     */
    //@ApiOperation("货柜到市场核对")
    //@PreAuthorize("@ss.hasPermi('ctn:addCtnReahedMarketCheck:add')" )
    //@Log(title = "货柜到市场核对" , businessType = BusinessType.INSERT)
    //@PostMapping("/addCtnReahedMarketCheck")
    //public AjaxResult addCtnReahedMarketCheck(@Valid @RequestBody GpNigeriaMarketCheckAdd gpNigeriaMarketCheckAdd) {
    //    if (gpNigeriaMarketCheckAdd == null) {
    //        return AjaxResult.parameterError();
    //    }
    //    if (gpNigeriaMarketCheckAdd.getCtnOrderId()==null){
    //        return AjaxResult.error("ctnOrderId is null");
    //    }
    //
    //    if (gpNigeriaMarketCheckAdd.getReceiptId()==null){
    //        return AjaxResult.error("receiptId is null");
    //    }
    //
    //    return toAjax(iGpNigeriaMarketCheckService.addCtnReahedMarketCheck(gpNigeriaMarketCheckAdd) ? 1 : 0);
    //}
}
